﻿using System;
using System.Collections.Generic;
using System.Text;
using System.Data;
using PPAP.Model;
using System.Data.OracleClient;

namespace PPAP.DAO
{
    public class PPAPNewsDAO
    {
        public PPAPNewsDAO() { }

        /// <summary>
		/// 增加一条数据
		/// </summary>
        public int Add(PPAPNews model)
		{
            model.ID = DbHelperOra.GetMaxID("ID", "ppapnews");
			StringBuilder strSql=new StringBuilder();
			strSql.Append("insert into PPAPNEWS(");
			strSql.Append("ID,CONTENT,TITLE,CREATETIME,CREATEDBY,TYPE)");
			strSql.Append(" values (");
			strSql.Append(":ID,:CONTENT,:TITLE,:CREATETIME,:CREATEDBY,:TYPE)");
			OracleParameter[] parameters = {
					new OracleParameter(":ID", OracleType.Number,4),
					new OracleParameter(":CONTENT", OracleType.VarChar,4000),
					new OracleParameter(":TITLE", OracleType.VarChar,350),
					new OracleParameter(":CREATETIME", OracleType.DateTime),
					new OracleParameter(":CREATEDBY", OracleType.VarChar,150),
					new OracleParameter(":TYPE", OracleType.Number,4)};
			parameters[0].Value = model.ID;
			parameters[1].Value = model.CONTENT;
			parameters[2].Value = model.TITLE;
			parameters[3].Value = model.CREATETIME;
			parameters[4].Value = model.CREATEDBY;
			parameters[5].Value = model.TYPE;

		  return DbHelperOra.ExecuteSql(strSql.ToString(),parameters)>0?int.Parse(model.ID.ToString()):0;
		}
		/// <summary>
		/// 更新一条数据
		/// </summary>
        public int Update(PPAPNews model)
		{
			StringBuilder strSql=new StringBuilder();
			strSql.Append("update PPAPNEWS set ");
			strSql.Append("ID=:ID,");
			strSql.Append("CONTENT=:CONTENT,");
			strSql.Append("TITLE=:TITLE,");
			strSql.Append("CREATETIME=:CREATETIME,");
			strSql.Append("CREATEDBY=:CREATEDBY,");
			strSql.Append("TYPE=:TYPE");
			strSql.Append(" where id="+model.ID);
			OracleParameter[] parameters = {
					new OracleParameter(":ID", OracleType.Number,4),
					new OracleParameter(":CONTENT", OracleType.VarChar,4000),
					new OracleParameter(":TITLE", OracleType.VarChar,350),
					new OracleParameter(":CREATETIME", OracleType.DateTime),
					new OracleParameter(":CREATEDBY", OracleType.VarChar,150),
					new OracleParameter(":TYPE", OracleType.Number,4)};
			parameters[0].Value = model.ID;
			parameters[1].Value = model.CONTENT;
			parameters[2].Value = model.TITLE;
			parameters[3].Value = model.CREATETIME;
			parameters[4].Value = model.CREATEDBY;
			parameters[5].Value = model.TYPE;

			return DbHelperOra.ExecuteSql(strSql.ToString(),parameters);
		}

		/// <summary>
		/// 删除一条数据
		/// </summary>
		public int Delete(int id)
		{
			//该表无主键信息，请自定义主键/条件字段
			StringBuilder strSql=new StringBuilder();
			strSql.Append("delete from PPAPNEWS ");
			strSql.Append(" where id="+id);
			OracleParameter[] parameters = {
};

			return DbHelperOra.ExecuteSql(strSql.ToString(),parameters);
		}


		/// <summary>
		/// 得到一个对象实体
		/// </summary>
        public PPAPNews GetModel(int id)
		{
			//该表无主键信息，请自定义主键/条件字段
			StringBuilder strSql=new StringBuilder();
			strSql.Append("select ID,CONTENT,TITLE,CREATETIME,CREATEDBY,TYPE from PPAPNEWS ");
			strSql.Append(" where id="+id);
			OracleParameter[] parameters = {
};

            PPAPNews model = new PPAPNews();
			DataSet ds=DbHelperOra.Query(strSql.ToString(),parameters);
			if(ds.Tables[0].Rows.Count>0)
			{
				if(ds.Tables[0].Rows[0]["ID"].ToString()!="")
				{
					model.ID=int.Parse(ds.Tables[0].Rows[0]["ID"].ToString());
				}
				model.CONTENT=ds.Tables[0].Rows[0]["CONTENT"].ToString();
				model.TITLE=ds.Tables[0].Rows[0]["TITLE"].ToString();
				if(ds.Tables[0].Rows[0]["CREATETIME"].ToString()!="")
				{
					model.CREATETIME=DateTime.Parse(ds.Tables[0].Rows[0]["CREATETIME"].ToString());
				}
				model.CREATEDBY=ds.Tables[0].Rows[0]["CREATEDBY"].ToString();
				if(ds.Tables[0].Rows[0]["TYPE"].ToString()!="")
				{
					model.TYPE=int.Parse(ds.Tables[0].Rows[0]["TYPE"].ToString());
				}
				return model;
			}
			else
			{
				return null;
			}
		}

		/// <summary>
		/// 获得数据列表
		/// </summary>
		public DataSet GetList(string strWhere)
		{
			StringBuilder strSql=new StringBuilder();
            strSql.Append(" select rownum,t.* from (select ID,CONTENT,TITLE,CREATETIME,CREATEDBY,TYPE from ppapnews order by createtime desc)t ");
            //strSql.Append(" FROM PPAPNEWS ");
			if(strWhere.Trim()!="")
			{
				strSql.Append(" where "+strWhere);
			}
			return DbHelperOra.Query(strSql.ToString());
		}

		/*
		/// <summary>
		/// 分页获取数据列表
		/// </summary>
		public DataSet GetList(int PageSize,int PageIndex,string strWhere)
		{
			OracleParameter[] parameters = {
					new OracleParameter(":tblName", OracleType.VarChar, 255),
					new OracleParameter(":fldName", OracleType.VarChar, 255),
					new OracleParameter(":PageSize", OracleType.Number),
					new OracleParameter(":PageIndex", OracleType.Number),
					new OracleParameter(":IsReCount", OracleType.Clob),
					new OracleParameter(":OrderType", OracleType.Clob),
					new OracleParameter(":strWhere", OracleType.VarChar,1000),
					};
			parameters[0].Value = "PPAPNEWS";
			parameters[1].Value = "ID";
			parameters[2].Value = PageSize;
			parameters[3].Value = PageIndex;
			parameters[4].Value = 0;
			parameters[5].Value = 0;
			parameters[6].Value = strWhere;	
			return DbHelperOra.RunProcedure("UP_GetRecordByPage",parameters,"ds");
		}*/

		
    }
}
